set more off

*** memphis ***

use "dataset_memphis wave 1", clear

foreach x in ideoawg ideocoll ideohodges ideowalk ideowebb ideowilliams {
	replace `x' = 4 if mi(`x')
}

gen ideononstrickland = (ideoawg + ideocoll + ideohodges + ideowalk + ideowebb + ideowilliams)/6
gen spatialadvantage = (abs(ideoawg -ideoself) + abs(ideocoll -ideoself) + abs(ideohodges -ideoself)+ abs(ideowalk -ideoself)+ abs(ideowebb-ideoself) + abs(ideowilliams-ideoself))/6 - abs(ideostrickland - ideoself) 

local X white female democrat homeowner college age

foreach x in spatialadvantage `X' {
	su `x'
	replace `x' = (`x'-r(min))/(r(max)-r(min))
}

eststo clear

eststo: reg votestrickland spatialadvantage
eststo: reg votestrickland spatialadvantage `X'

*** nashville ***

use "dataset_nashville wave 1", clear

foreach x in kane bone rebrovick freeman fox gentry {
	replace ideo`x'1 = 4 if mi(ideo`x'1)
}
gen ideononbarry = (ideokane1 + ideobone1 + ideorebrovick1 + ideofreeman1 + ideofox1 + ideogentry1)/6 
gen spatialadvantage = (abs(ideokane1-ideoself1) + abs(ideobone1-ideoself1) + abs(ideorebrovick1 - ideoself1) + abs(ideofreeman1-ideoself1) + abs(ideofox1-ideoself1) + abs(ideogentry1-ideoself1))/6 - abs(ideobarry1- ideoself1) 

foreach x in spatialadvantage `X' {
	su `x'
	replace `x' = (`x'-r(min))/(r(max)-r(min))
}

rename votebarry1 votebarry
eststo: reg votebarry spatialadvantage
eststo: reg votebarry spatialadvantage `X'

*** nashville panel ***

use "dataset_nashville panel", clear

foreach x in kane bone rebrovick freeman fox gentry {
	replace ideo`x'1 = 4 if mi(ideo`x'1)
	replace ideo`x'2 = 4 if mi(ideo`x'2)
}
gen ideononbarry1 = (ideokane1 + ideobone1 + ideorebrovick1 + ideofreeman1 + ideofox1 + ideogentry1)/6 
gen ideononbarry2 = (ideokane2 + ideobone2 + ideorebrovick2 + ideofreeman2 + ideofox2 + ideogentry2)/6 
gen spatialadvantage1 = (abs(ideokane1-ideoself1) + abs(ideobone1-ideoself1) + abs(ideorebrovick1 - ideoself1) + abs(ideofreeman1-ideoself1) + abs(ideofox1-ideoself1) + abs(ideogentry1-ideoself1))/6 - abs(ideobarry1- ideoself1) 
gen spatialadvantage2 = (abs(ideokane2-ideoself2) + abs(ideobone2-ideoself2) + abs(ideorebrovick2 - ideoself2) + abs(ideofreeman2-ideoself2) + abs(ideofox2-ideoself2) + abs(ideogentry2-ideoself2))/6 - abs(ideobarry2- ideoself2) 

gen dspat = spatialadvantage2 - spatialadvantage1

foreach x in dspat `X' {
	su `x'
	replace `x' = (`x'-r(min))/(r(max)-r(min))
}

eststo: reg votebarry2 dspat
eststo: reg votebarry2 dspat votebarry1

*** output table ***

gen spatialadvantage = .
gen votebarry = .
gen votestrickland = .
label variable spatialadvantage "Spatial advantage"
label variable dspat "$\Delta$ spatial advantage"
label variable votebarry1 "Lagged vote"
label variable votebarry2 "Barry vote"
label variable votebarry "Barry vote"
label variable votestrickland "Strickland vote"

#delimit;

esttab
	using "table A3.tex"
	,
		order(spatialadvantage dspat)
		booktabs
		width(\linewidth)
		replace
		se 
		compress 
		cells(b(fmt(2) star) se(par))
		collabels(none)
		label		
		mgroups(
			"Memphis"
			"Nashville"
			"Nashville (panel)"
			 ,
				pattern(1 0 1 0 1 0)
				prefix(\multicolumn{@span}{c}{) suffix(}) 
				span 
				erepeat(\cmidrule(lr){@span})
		)		
		;
		
#delimit cr
